package com.j3.db;

import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.util.Properties;

import javax.sql.DataSource;

public class SimonDataSource implements DataSource {
	private String jndiName;
	private String driverClass;
	private String driverUrl;
	private String username;
	private String password;
	private int connCacheCount = 0; // default no cache
	private int connMinCount = 0;
	private int loginTimeout = 10;
	private PrintWriter out = null;

	public SimonDataSource(String driverClass, String driverUrl, String username, String password) {
		this.driverClass = driverClass;
		this.driverUrl = driverUrl;
		this.username = username;
		this.password = password;
	}

	public SimonDataSource(String driverClass, String driverUrl) {
		this.driverClass = driverClass;
		this.driverUrl = driverUrl;
	}

	public Connection getConn() throws SQLException, ClassNotFoundException {
		Properties props = new Properties();
		Class.forName(this.driverClass);
		DriverManager.setLoginTimeout(this.getLoginTimeout());
		props.put("user", username);
		props.put("password", password);
		return DriverManager.getConnection(this.driverUrl, props);
	}

	synchronized public Connection getConnection() throws SQLException {
		Connection conn = null;
		try {
			conn = getConn();
		} catch (ClassNotFoundException e) {
			e.printStackTrace();
		}
		return conn;
	}

	synchronized public Connection getConnection(String username, String password) throws SQLException {
		this.username = username;
		this.password = password;
		return getConnection();
	}

	public PrintWriter getLogWriter() throws SQLException {
		return this.out;
	}

	public int getLoginTimeout() throws SQLException {
		return this.loginTimeout;
	}

	public void setLogWriter(PrintWriter out) throws SQLException {
		this.out = out;
	}

	public void setLoginTimeout(int seconds) throws SQLException {
		this.loginTimeout = seconds;
	}

	public int getConnCacheCount() {
		return connCacheCount;
	}

	public void setConnCacheCount(int connCacheCount) {
		this.connCacheCount = connCacheCount;
	}

	public int getConnMinCount() {
		return connMinCount;
	}

	public void setConnMinCount(int connMinCount) {
		this.connMinCount = connMinCount;
	}

	public String getDriverClass() {
		return driverClass;
	}

	public void setDriverClass(String driverClass) {
		this.driverClass = driverClass;
	}

	public String getDriverUrl() {
		return driverUrl;
	}

	public void setDriverUrl(String driverUrl) {
		this.driverUrl = driverUrl;
	}

	public String getPassword() {
		return password;
	}

	public void setPassword(String password) {
		this.password = password;
	}

	public String getUsername() {
		return username;
	}

	public void setUsername(String username) {
		this.username = username;
	}

	public String getJndiName() {
		return jndiName;
	}

	public void setJndiName(String jndiName) {
		this.jndiName = jndiName;
	}

	public boolean isWrapperFor(Class<?> arg0) throws SQLException {
		// TODO Auto-generated method stub
		return false;
	}

	public <T> T unwrap(Class<T> arg0) throws SQLException {
		// TODO Auto-generated method stub
		return null;
	}

}